package levelrewind.android.app.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

/**
 * Utility class supporting another class.
 * @author Sureewan, Martin
 */
public class Util {
	
	/**
	 * Enumeration for person's pictures in difference emotion
	 */
	public enum Emotion{NORMAL, HAPPY, SAD}

	/**
	 * Check for device is connect to internet
	 * @param Context c
	 * @return true, is online. 
	 * 		   false, is offline.
	 */
	public static boolean isOnline(Context c) {
        ConnectivityManager cm =
            (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
	
	/**
	 * Method to set bundle object to send between activity
	 * @param Object input
	 * @return Bundle
	 */
	public static Bundle setBundleObject(Object input) {
		LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
		obj.put("hashmapkey", input);
		Bundle b = new Bundle();
		b.putSerializable("bundleobj", obj);
		
		return b;
	}
	
	/**
	 * Method to get bundle object that has been sent from another activity
	 * @param Bundle b
	 * @return Object
	 */
	public static Object getBundleObject(Bundle b) {		
		@SuppressWarnings("unchecked")
		HashMap<String, Object> getobj = (HashMap<String, Object>) b.getSerializable("bundleobj");
		getobj.get("hashmapkey");
		
		return getobj.get("hashmapkey");
	}
	
	/**
	 * Method to set bundle array object to send between activity
	 * @param Object input
	 * @return Bundle
	 */
	public static Bundle setBundleArrayObject(Object[] input) {
		LinkedHashMap<String, Object[]> obj = new LinkedHashMap<String, Object[]>();
		obj.put("hashmapkey", input);
		Bundle b = new Bundle();
		b.putSerializable("bundleobj", obj);
		
		return b;
	}
	
	/**
	 * Method to get bundle array object that has been sent from another activity
	 * @param Bundle b
	 * @return Object
	 */
	public static Object[] getBundleArrayObject(Bundle b) {		
		@SuppressWarnings("unchecked")
		HashMap<String, Object[]> getobj = (HashMap<String, Object[]>) b.getSerializable("bundleobj");
		getobj.get("hashmapkey");
		
		return getobj.get("hashmapkey");
	}
	
	/**
	 * Convert from string name to Emotion object
	 * @param String name
	 * @return Emotion af person
	 */
	public static Emotion getEmotionByName(String name) {
		if (name == null || name == "") return null;
		else if (name.toUpperCase().equals(Emotion.NORMAL.name().toUpperCase())) return Emotion.NORMAL;
		else if (name.toUpperCase().equals(Emotion.HAPPY.name().toUpperCase())) return Emotion.HAPPY;
		else if (name.toUpperCase().equals(Emotion.SAD.name().toUpperCase())) return Emotion.SAD;
		
		return null;
	}
	
	/**
	 * Get font from asset to use in application
	 * @param Context c
	 * @return Typeface as font type
	 */
	public static Typeface customFont(Context c) {
		Typeface font = Typeface.createFromAsset(c.getAssets(), "fonts/Gill Sans MT.ttf");
		return font;
	}
	
	/**
	 * Convert inputStream to string
	 * @param InputStream is
	 * @return String
	 */
	public static String convertStreamToString(InputStream is) {
	    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	    StringBuilder sb = new StringBuilder();
	    String line = null;
	 
	    try {
			while ((line = reader.readLine()) != null) {
			     sb.append(line);
			 }
			
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	    return sb.toString();
	 }
	
	/**
	 * Format number with '.' separator likes currency
	 * @param value
	 * @return String with format value
	 */
	public static String formatNumber(long value) {
		DecimalFormat fmt = new DecimalFormat();
		DecimalFormatSymbols fmts = new DecimalFormatSymbols();
		fmts.setGroupingSeparator('.');

		fmt.setGroupingSize(3);
		fmt.setGroupingUsed(true);
		fmt.setDecimalFormatSymbols(fmts);
		return fmt.format(value);
	}
	
	public static Long sum(ArrayList<Long> list) {
	     Long sum = 0L; 
	     for (Long value : list)
	         sum += value;
	     return sum;
	}	
	
	/**
	 * Converting imageURL to Bitmap.
	 * @param imageURL
	 * @return Bitmap
	 */
	public static Bitmap getUserPicture(String imageURL) {
		Bitmap picture = null;
		try {
			picture = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
			//Resize picture to 50x50
			int width = 50, height = 50;
			picture = Bitmap.createScaledBitmap(picture, width, height, true); 
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		return picture;
	}
	 
}
